package com.zx.jhpt.datamart;

import com.zx.framework.core.pagination.Pagination;
import com.zx.framework.plat.login.LoginAdapterController;
import com.zx.framework.plat.user.vo.UserVo;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/7.
 */
@Controller
@RequestMapping("/dataMart")
public class DataMartController extends LoginAdapterController {
    private static final Logger LOGGER = Logger.getLogger(DataMartController.class);
    @Resource
    private DataMartService dataMartService;

    @InitBinder("dataMart")
    public void replyBinder(WebDataBinder binder) throws Exception {
        binder.setFieldDefaultPrefix("dataMart.");
    }

    /**
     * 查询回复
     *
     * @return 回复信息的分页对象
     */
    @RequestMapping("/page")
    @ResponseBody
    public Pagination<DataMart> page(String tableName, Integer page, Integer rows) {
        UserVo loginUser = getLoginUser();
        page = page == null ? 1 : page;
        rows = rows == null ? 10 : rows;
        Pagination<DataMart> dataMartPagination = null;
        DataMart dataMart = MarketEngine.getDataMart(tableName);
        dataMartPagination = dataMartService.page(dataMart, loginUser, page, rows);
        return dataMartPagination;
    }

    /**
     * 表名
     *
     * @return
     */
    @RequestMapping("/table")
    @ResponseBody
    public List<DataMart> tableNames() {
        return MarketEngine.getAll();
    }

    /**
     * 数据核查
     *
     * @return
     */
    @RequestMapping("/hc")
    @ResponseBody
    public Map<String, Object> hc(String hcId, String tableName) {
        try {
            UserVo loginUser = getLoginUser();
            String zxStatus = dataMartService.hc(hcId, tableName,loginUser);
            return clientMap().success().message(zxStatus).getMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return clientMap().failure().message(e.getMessage()).getMap();
        }
    }
}
